//
//  LXUser.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/15.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXUser.h"
@interface LXUser()
@end
@implementation LXUser
+(instancetype)shareUser{
    static dispatch_once_t onceToken;
    static LXUser * user = nil;
    dispatch_once(&onceToken, ^{
        user = [[[self class] alloc] _init];
        [user _readFromDisk];
    });
    return user;
}

-(id)_init{
    if(self = [super init]){
        self.username = @"";
        self.mobile = @"";
        self.password = @"";
        self.invitecode = @"";
        self.qqid = @"";
        self.weixinid = @"";
        self.weiboid = @"";
        self.alipayid = @"";
        self.uid = @"";
        self.nickname = @"";
        self.user_id = @"";
        self.headerImage = @"";
        self.sex = @"保密";
    }
    return self;
}


-(void)setHeaderImage:(NSString *)headerImage{
    if(headerImage&&headerImage.length!=0&& (![[headerImage uppercaseString] containsString:@"HTTP"])){
      headerImage = [headerImage addHeader:[NSString stringWithFormat:@"%@/",host]];
    }
    _headerImage = headerImage;
}
//-(NSString*)headerImage{
//    if(![[_headerImage uppercaseString] containsString:@"HTTP"]){
//        _headerImage = [_headerImage addHeader:[NSString stringWithFormat:@"%@/",host]];
//    }
//    return _headerImage;
//}
+(NSDictionary*)mj_replacedKeyFromPropertyName{
    return @{@"id":@"ID",@"user_img":@"headerImage"};
}

-(void)setCommission:(NSString *)commission{
    _commission = commission;
}

-(void)setUserInfo:(NSDictionary *)userInfo{
    
    if([userInfo isKindOfClass:[NSDictionary class]]){
        for (NSString * key in userInfo.allKeys) {
            id value = userInfo[key];
            if([value isKindOfClass:[NSNumber class]]){
                [self setValue:[value stringValue] forKey:key];
            }
            else if([value isKindOfClass:[NSString class]]){
                [self setValue:value forKey:key];
            }
            else if ([value isKindOfClass:[NSNull class]]){
                [self setValue:@"" forKey:key];
            }
        }
    }
    
}

+(void)upDataUserInfoWith:(NSDictionary*)userInfo{
    LXUser * user = [LXUser shareUser];
    if([userInfo isKindOfClass:[NSDictionary class]]){
        for (NSString * key in userInfo.allKeys) {
            id value = userInfo[key];
            if([value isKindOfClass:[NSNumber class]]){
                [user setValue:[value stringValue] forKey:key];
            }
            else if([value isKindOfClass:[NSString class]]){
                [user setValue:value forKey:key];
            }
            else if ([value isKindOfClass:[NSNull class]]){
                [user setValue:@"" forKey:key];
            }
        }
    }
    [NSUserDefaults setString:user.mobile forKey:KeyOfUserMobile];
    [NSUserDefaults setString:user.user_id forKey:KeyOfUserId];
    [NSUserDefaults setString:user.nickname forKey:KeyOfUserNickname];
    [NSUserDefaults setString:user.headerImage forKey:KeyOfUserHeaderImage];
    [NSUserDefaults setBool:YES forKey:KeyOfUserLoginState];
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"%s",__func__);
    if([key isEqualToString:@"user_img"]){
        if([value length]!=0)
        [self setValue:value forKey:@"headerImage"];
    }
}

-(void)setValue:(id)value forKey:(NSString *)key{
    [super setValue:value forKey:key];
}

+(void)logout{
    LXUser * user = [LXUser shareUser];
    user.username = @"";
    user.mobile = @"";
    user.password = @"";
    user.invitecode = @"";
    user.qqid = @"";
    user.weixinid = @"";
    user.weiboid = @"";
    user.alipayid = @"";
    user.uid = @"";
    user.nickname = @"";
    user.user_id = @"";
    user.headerImage = @"";
    user.sex = @"保密";
    [NSUserDefaults setString:user.mobile forKey:KeyOfUserMobile];
    [NSUserDefaults setString:user.user_id forKey:KeyOfUserId];
    [NSUserDefaults setString:user.nickname forKey:KeyOfUserNickname];
    [NSUserDefaults setString:user.headerImage forKey:KeyOfUserHeaderImage];
    [NSUserDefaults setBool:NO forKey:KeyOfUserLoginState];
}

-(void)_readFromDisk{
    
}

@end

NSString * const KeyOfUserName          = @"userName";
NSString * const KeyOfUserMobile        = @"mobile";
NSString * const KeyOfUserPassword      = @"password";
NSString * const KeyOfUserInvitecode    = @"invitecode";
NSString * const KeyOfUserQQid          = @"qqid";
NSString * const KeyOfUserWeixinid      = @"weixinid";
NSString * const KeyOfUserWeiboid       = @"weiboid";
NSString * const KeyOfUserAlipayid      = @"alipayid";
NSString * const KeyOfUserUid           = @"uid";
NSString * const KeyOfUserId            = @"user_id";
NSString * const KeyOfUserNickname      = @"nickname";
NSString * const KeyOfUserHeaderImage   = @"headerImage";
NSString * const KeyOfUserBeannums      = @"bean_nums";
NSString * const KeyOfUserOrdernotpaye  = @"ordernotpaye";
NSString * const KeyOfUserOrederall     = @"orederall";
NSString * const KeyOfUserCommission    = @"commission";
NSString * const KeyOfUserLoginState    = @"LoginState";
